/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

/**
 *
 * @author Daniel
 */
public class Controller extends javax.swing.JFrame {
    DTrem dtrem;
    TremConect tc;
    /**
     * Creates new form Controller
     * @param dtrem
     * @param tc
     */
    public Controller(DTrem dtrem, TremConect tc) {
        this.dtrem = dtrem;
        this.tc = tc;
        initComponents();
    }

    /**
     *
     */
    public void stopConections(){
        jButton1.setEnabled(false);
        ip.setEnabled(false);
    }

    /**
     *
     * @param ip
     */
    public void setIpText(String ip){
        this.ip.setText(ip);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        velocidade = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        ip = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Controlador");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setText("Velocidade");

        velocidade.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        velocidade.setMajorTickSpacing(5);
        velocidade.setMaximum(10);
        velocidade.setMinorTickSpacing(1);
        velocidade.setPaintLabels(true);
        velocidade.setPaintTicks(true);
        velocidade.setSnapToTicks(true);
        velocidade.setValue(0);
        velocidade.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                velocidadeStateChanged(evt);
            }
        });

        jLabel2.setText("IP");

        jButton1.setText("conect");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addComponent(velocidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ip, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(velocidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void velocidadeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_velocidadeStateChanged
        if(dtrem.getTremNumber()==1)
            dtrem.setVel1(velocidade.getValue());
        else dtrem.setVel2(velocidade.getValue());
    }//GEN-LAST:event_velocidadeStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(tc.conect(ip.getText())){
            jButton1.setEnabled(false);
            ip.setEnabled(false);
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ip;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSlider velocidade;
    // End of variables declaration//GEN-END:variables
}
